﻿using System;
using Cuce.ReportingServices.SSRSWS;
using Cuce.ReportingServices.UnitTests.Mock;
using NUnit.Framework;

namespace Cuce.ReportingServices.UnitTests
{

    /// <summary>
    /// 
    /// </summary>
    [TestFixture]
    public class UnitTestWrapperTest
    {

        private UnitTestWrapper i = null;
        private const string _validReportServer = "http://melwdscrr01/ReportServer";
        private const string _testReportName = "testreport";

        /// <summary>
        /// Tests if URL set.
        /// </summary>
        [Test]
        public void TestIfURLSet()
        {
            try
            {

                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

                i.TestReport(new ReportName(_testReportName, "/"));
                
                Assert.IsTrue(true);
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(false);
            }

        }


        /// <summary>
        /// Tests the return results column.
        /// </summary>
        [Test]
        public void TestReturnResultsColumn()
        {
            try
            {

                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };


                var h = i.TestReport(new ReportName(_testReportName, "/"));

                Assert.IsTrue(h[0].Columns[0].ToString() =="test");
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(false);
            }

        }

        /// <summary>
        /// Tests the return results row.
        /// </summary>
        [Test]
        public void TestReturnResultsRow()
        {
            try
            {

                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

                var h = i.TestReport(new ReportName(_testReportName, "/"));

                Assert.IsTrue(h[0].ToString() == "test, test, test, test");
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(false);
            }

        }

        /// <summary>
        /// Tests the return results rows.
        /// </summary>
        [Test]
        public void TestReturnResultsRows()
        {
            try
            {

                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

                var h = i.TestReport(new ReportName(_testReportName, "/"));

                Assert.IsTrue(h.ToString() == "test, test, test, test\ntest, test, test, test\n");

            }
            catch (ArgumentException)
            {
                Assert.IsTrue(false);
            }

        }

        /// <summary>
        /// Tests if URL not set.
        /// </summary>
        [Test]
        public void TestIfURLNotSet()
        {
            try
            {
                i = new UnitTestWrapper(new ReportingServicesMock());

                i.TestReport(new ReportName(_testReportName, "/"));
               
                
                Assert.IsTrue(false);
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }
            
        }

        /// <summary>
        /// Tests if report name blank.
        /// </summary>
        [Test]
        public void TestIfReportNameBlank()
        {
            try
            {
                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };
                
                i.TestReport(new ReportName("", "/"));

                Assert.IsTrue(false);
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }

           
        }


        /// <summary>
        /// Tests if parameter list is null.
        /// </summary>
        [Test]
        public void TestIfParameterListIsNull()
        {
            try
            {
                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

                i.TestReport(new ReportName("test", "/"), null);

                Assert.IsTrue(false);
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }


        }

        /// <summary>
        /// Tests if parameter list is null.
        /// </summary>
        [Test]
        public void TestIfParameterListIsNull2()
        {
            try
            {
                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };
                
                i.TestReport(null, null);

                Assert.IsTrue(false);
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }


        }

        /// <summary>
        /// Tests if parameter list is null.
        /// </summary>
        [Test]
        public void TestIfParameterListIsNull3()
        {
            try
            {
                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

                i.TestReport(null, null, null);

                Assert.IsTrue(false);
            }
            catch (ArgumentException)
            {
                Assert.IsTrue(true);
            }


        }

        /// <summary>
        /// Tests the parameter list.
        /// </summary>
        [Test]
        public void TestParameterList()
        {

            i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

            i.TestReport(new ReportName("test", "/"), new ParameterValue[] {});

            Assert.IsTrue(true);



        }

        /// <summary>
        /// Tests if RS exception thrown.
        /// </summary>
        [Test]
        public void TestIfRSExceptionThrown()
        {
            try
            {
                i = new UnitTestWrapper(new ReportingServicesMock()) {ReportServer = _validReportServer};

                var list = new ParameterValue[1];
                list[0] = new ParameterValue {Name = "Error"};

                i.TestReport(new ReportName("test", "/"), list);

                Assert.IsTrue(false);
            }
            catch (ReportException)
            {
                Assert.IsTrue(true);
            }


        }

        /// <summary>
        /// Tests if RS warnings thrown.
        /// </summary>
        [Test]
        public void TestIfRSWarningsThrown()
        {
            try
            {
                i = new UnitTestWrapper(new ReportingServicesMock()) { ReportServer = _validReportServer };

                var list = new ParameterValue[1];
                list[0] = new ParameterValue {Name = "Warning"};

                i.TestReport(new ReportName("test", "/"), list);

                Assert.IsTrue(false);
            }
            catch (ReportException e)
            {

                if (e.Warnings.Length > 0)
                {
                    Assert.IsTrue(e.Warnings[0].Message == "Warning");
                }
                else
                    Assert.IsTrue(false);
            }


        }

       
      
    }
}
